<?
class EditProvinceForm extends Form{
	private $province=array();
	
	function EditProvinceForm(){
		Form::Form('EditProvinceForm');

		if(Url::get('cmd')=='edit'){			
			$id = (int)Url::get('id');
			if($id){
				$this->province=DB::select('province','id='.$id);
			}
			
			if(!$this->province)
			Url::redirect_current();
		}
		else{
			$this->province=array(
									'name'		=>'',
									'brief_name'=>'',
									'position'	=>'',
									'status'	=>'HIDE',
									'area'		=>1,
									'total_item'=>0
								);
		}
	}
	
	function on_submit(){
		
		$name		=Url::get('name');
		$brief_name	=Url::get('brief_name');
		$status		=Url::get('status',"HIDE");
		$area		=(int)Url::get('area');
		$position	=(int)Url::get('position',0);
		
		$this->checkFormInput('Tên đối tác','name',$name,'str',true,'',2,75);
		$this->checkFormInput('Vị trí','position',$position,'int',true,'',1);
		
		if($brief_name=='') $brief_name=$name;
		
		if(!$this->errNum){
			$new_row =array(
								'name'=>$name,
								'brief_name'=>$brief_name,
								'position'	=>$position,
								'status'	=>$status,
								'area'		=>$area);
								
			if(Url::get('cmd')=='edit'){			
				DB::update_id('province', $new_row,$this->province['id']);
			}
			else{
				DB::insert('province', $new_row);		
			}
			AZLib::getProvinces(1,true);
			Url::redirect_current();
		}
	}	
	
	function draw(){	
		global $display;
		$this->beginForm();
		
		if(Url::get('cmd')=='edit')
			$display->add('mode',"Sửa");
		else
			$display->add('mode',"Thêm");
			
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->add('name',Url::get('name',$this->province['name']));
		$display->add('brief_name',Url::get('brief_name',$this->province['brief_name']));
		$display->add('area',Url::get('url',$this->province['area']));
		$display->add('position',(int)Url::get('position',$this->province['position']));
		$display->add('total_item',(int)Url::get('position',$this->province['total_item']));
		$display->add('option_area',AZLib::getOption(array(1=>'Bắc',2=>'Trung',3=>'Nam'),Url::get('area',$this->province['area'])));
		$display->add('option_status',AZLib::getOption(array('SHOW'=>'Hiện','HIDE'=>'Ẩn','HOME'=>'HOME'),Url::get('status',$this->province['status'])));
		$display->output('edit');
		$this->endForm();
	}
}
?>